package com.ndp.fb.rdb.db.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ndp.fb.rdb.api.MenuRdbService;
import com.ndp.fb.rdb.db.DaoTemplate;
import com.ndp.fb.rdb.db.impl.mapper.DefaultRowMapper;
import com.ndp.fb.rdb.model.Menu;
import com.ndp.fb.util.ListUtil;
import com.ndp.fb.util.SQLUtil;

import java.util.List;

/**
 * Created by elvisxiong on 7/10/15.
 */
@Service
public class MenuRdbServiceImpl extends DaoTemplate implements MenuRdbService{

    @Override
    public List<Menu> getMenuByRoleAndFidCN(List<Long> roleIds, Long fid) {
        String sql = "select DISTINCT m.* from menu m,role_menu om where m.id = om.menu_id and m.f_id = ? and m.status = ? and om.role_id in (" + SQLUtil.appendPlaceholder(roleIds.size()) + ") ";
        return getMenus(roleIds, fid, sql);
    }

    @Override
    public List<Menu> getMenuByRoleAndFidUS(List<Long> roleIds, Long fid) {
        String sql = "select DISTINCT m.en_name as name,m.*  from menu m,role_menu om where m.id = om.menu_id and m.f_id = ? and m.status = ? and om.role_id in (" + SQLUtil.appendPlaceholder(roleIds.size()) + ") ";
        return getMenus(roleIds, fid, sql);
    }



    private List<Menu> getMenus(List<Long> roleIds, Long fid, String sql) {
        Object[] param = new Object[roleIds.size() + 2];
        param[0] = fid;
        param[1] = 1;
        if (ListUtil.isEmpty(roleIds))
            return null;
        else {
            for (int m = 0; m < roleIds.size(); m++) {
                param[m + 2] = roleIds.get(m);
            }
        }
        return getJdbcTemplate().query(sql, param, DefaultRowMapper.autoRowMapper(Menu.class));
    }
}
